<?php
/**
 * @version		0.2.0 joomla zmluvy $
 * @package		com_zjmluvy
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author    AdrianKo		
 * @website   http://code.google.com/p/joomla-zmluvy
 *
 *
 *1.3.2012
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');
jimport( 'joomla.filesystem.file' );

class jzmluvyControllerUpravazaznamu extends JController
{

	function __construct()
	{
		parent::__construct();
		
		// Registrace extra dotazu
		$this->registerTask( 'add'  , 	'display'  );
		$this->registerTask( 'edit'  , 	'display'  );
		$this->registerTask( 'cancel'  , 	'display'  );
		$this->registerTask( 'apply', 	'save'  );
		$this->registerTask( 'remove', 	'delete'  );
		$this->registerTask( 'deletefile', 	'deletefile'  );
    $this->registerTask( 'deletefile2', 	'deletefile2'  );
		$this->registerTask( 'archive'  , 	'archive'  );
		$this->registerTask( 'unarchive'  , 	'unarchive'  );
	}

  	/**
	 * Displays a view
	 */
	function display( )
	{		
    
    
    switch($this->getTask())
		{
			case 'add'     :
			{	JRequest::setVar( 'hidemainmenu', 1 );
//				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view', 'upravazaznamu' );
				JRequest::setVar( 'edit', false );
				
			} break;
			case 'edit'    :
			{
				$db 		=& JFactory::getDBO();
        
        $id = JRequest::getVar( 'id', '', '', 'array' );
    
        $query = 'SELECT state'
    			. ' FROM #__jzmluvy'
    			. ' WHERE id = '.$id[0]
      		;
    		$db->setQuery( $query );
    		$state = $db->loadResult();
				
				if ($state == -1) {
          $msg 	= 'Není možné upravovat archivovaný záznam!';
          $link = 'index.php?option=com_jzmluvy&view=jzmluvy';
          $this->setRedirect( $link, $msg );
          return false;
        }
        
        JRequest::setVar( 'hidemainmenu', 1 );
//				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view', 'upravazaznamu' );
				JRequest::setVar( 'edit', true );
			} break;
			
			case 'cancel'    :
			{
				JRequest::setVar( 'view', 'jzmluvy' );
			} break;
		}

		parent::display();
	}
	
		function save()
	{
		JRequest::checkToken() or die( 'Invalid Token' );
		
		$task		= JRequest::getVar('task');

		$post = JRequest::get( 'post' );
		$post['fulltext'] = JRequest::getVar( 'fulltext', '', 'post','string', JREQUEST_ALLOWRAW );
		$post['fulltext']	= str_replace( '<br>', '<br />', $post['fulltext'] );
		$post['file'] = JRequest::getVar('soubor', null, 'files', 'array');
    $post['file2'] = JRequest::getVar('soubor2', null, 'files2', 'array');

		$model = $this->getModel('upravazaznamu');
		
		$returnid = $model->store($post);

		if ($returnid) {
      
			switch ($task)
			{
				case 'apply' :
					$link = 'index.php?option=com_jzmluvy&view=upravazaznamu&hidemainmenu=1&id[]='.$returnid;
					break;

				default :
					$link = 'index.php?option=com_jzmluvy&view=jzmluvy';
					break;
			}
			$msg	= 'Záznam byl přidán či upraven';

		} else {

			$msg 	= '';
			$link = 'index.php?option=com_jzmluvy&view=jzmluvy';

		}

//		$model->checkin();

    if (JError::getError()->message != '') {
      $link = 'index.php?option=com_jzmluvy&view=upravazaznamu&hidemainmenu=1&task=edit&id[]='.$post['id'].'&catid='.$post['catid'].'&publish_up='.$post['publish_up'].'&publish_down='.$post['publish_down'].'&title='.$post['title'].'&created_by_alias='.$post['created_by_alias'].'&jedcislo='.$post['jedcislo'];  
    
    }

		$this->setRedirect( $link, $msg );
 	}
 	
  function delete () {
    JRequest::checkToken() or die( 'Invalid Token' );
    $params	= &JComponentHelper::getParams('com_jzmluvy');
    
    $db 		=& JFactory::getDBO();
    $id = JRequest::getVar( 'id', '', '', 'array' );
    
    $where = ' WHERE id IN('.implode($id,',').')';
    
    $query = 'SELECT *'
			. ' FROM #__jzmluvy'
			. $where
		;
		$db->setQuery( $query );
		$rows = $db->loadObjectList();
		
		foreach ($rows AS $row) {
		  
      if ($row->state == -1 && $params->get( 'mazat_archiv') == 0) { 
        $msg 	= 'Není možné smazat archivovaný záznam!';
        $link = 'index.php?option=com_jzmluvy&view=jzmluvy';
        $this->setRedirect( $link, $msg );
        return false;  
      }        
    }
		
		$files = array();
		$dir = JPATH_SITE.DS.$params->get( 'adresar');
    foreach ($rows AS $row) {
      if ($row->file != '') { 
        $files[] = $dir.$row->id.'-'.$row->filename.'.'.$row->extension;
      }        
    }
    $result = JFile::delete($files);
    
    $query = 'DELETE FROM #__jzmluvy'. $where;
    
    $db->setQuery($query);
    $result = $db->query();

    $msg 	= 'Záznamy byly vymazány';
		$link = 'index.php?option=com_jzmluvy&view=jzmluvy';
			
		$this->setRedirect( $link, $msg );
  }
  
    function deletefile () {
    JRequest::checkToken() or die( 'Invalid Token' );
    $params	= &JComponentHelper::getParams('com_jzmluvy');
    
    $db 		=& JFactory::getDBO();
    $id = JRequest::getVar( 'id', '', '', 'string' );
    
    $where = ' WHERE id = '.$id;
    
    $query = 'SELECT *'
			. ' FROM #__jzmluvy'
			. $where
		;
		
		$db->setQuery( $query );
		$row = $db->loadObject();
		
		$file = '';
		$dir = JPATH_SITE.DS.$params->get( 'adresar');
    $file = $dir.$row->id.'-'.$row->filename.'.'.$row->extension;        
    
    $result = JFile::delete($file);
    
    $query = 'UPDATE #__jzmluvy SET file = \'\', filename = \'\', extension = \'\''.$where;
    
    $db->setQuery($query);
    $result = $db->query();

    $msg 	= 'Soubor byl vymazán';
		$link = 'index.php?option=com_jzmluvy&view=upravazaznamu&hidemainmenu=1&id='.$id;
			
		$this->setRedirect( $link, $msg );
  }
  
  function archive () {
    JRequest::checkToken() or die( 'Invalid Token' );
    
    $db 		=& JFactory::getDBO();
    $id = JRequest::getVar( 'id', '', '', 'array' );
    
    $where = ' WHERE id IN('.implode($id,',').')';
    $where1 = ' WHERE id IN('.implode($id,',').') AND (publish_down = \'0000-00-00\' OR publish_down > CURRENT_DATE())';
    
    $query = 'UPDATE #__jzmluvy SET state = -1'
			. $where
		;
      
		$db->setQuery( $query );
    $db->setQuery($query);
    $result = $db->query();
    
		$query = ' UPDATE #__jzmluvy SET publish_down = CURRENT_DATE()'
		  . $where1
		;
      
		$db->setQuery( $query );
    $db->setQuery($query);
    $result = $db->query();

    $msg 	= 'Záznamy byly přesunuty do archivu';
		$link = 'index.php?option=com_jzmluvy&view=jzmluvy';
			
		$this->setRedirect( $link, $msg );
  }
  
  function unarchive () {
    JRequest::checkToken() or die( 'Invalid Token' );
    
    $db 		=& JFactory::getDBO();
    $id = JRequest::getVar( 'id', '', '', 'array' );
    
    $where = ' WHERE id IN('.implode($id,',').')';
    
    $query = 'UPDATE #__jzmluvy SET state = 0 '
			. $where
		;
		$db->setQuery( $query );
    $result = $db->query();

    $msg 	= 'Záznamy byly přesunuty z archivu';
		$link = 'index.php?option=com_jzmluvy&view=jzmluvy';
			
		$this->setRedirect( $link, $msg );
  }

}
?>